﻿using System.Reflection;

namespace Sidvall.Data
{
    public static class KeyValueManager
    {
        #region FromEnum

        public static KeyValueCollection<string, TValue> FromEnum<TValue>()
        {
            var enumType = typeof(TValue);
            return FromEnum<TValue>(enumType);
        }
        public static KeyValueCollection<string, TValue> FromEnum<TValue>(System.Type enumType)
        {
            var items = new KeyValueCollection<string, TValue>();
            var values = System.Enum.GetValues(enumType);
            var names = System.Enum.GetNames(enumType);
            var iMax = values.Length;
            for (int i = 0; i < iMax; i++)
            {
                var key = names[i];
                var fieldInfo = enumType.GetTypeInfo().GetDeclaredField(key);
                var attribute = fieldInfo.GetCustomAttribute(typeof(Sidvall.Serialization.DescriptionAttribute), false);
                if (attribute != null)
                    key = ((Sidvall.Serialization.DescriptionAttribute)attribute).Description;
                items.Add(key, (TValue)values.GetValue(i));
            }
            return items;
        }
        public static KeyValueCollection<string, long> FromEnum(string typeName)
        {
            var enumType = System.Type.GetType(typeName);
            return FromEnum(enumType);
        }
        public static KeyValueCollection<string, long> FromEnum(System.Type enumType)
        {
            return FromEnum<long>(enumType);
        }

        #endregion
        #region FromValue

        public static KeyValue<TKey, TValue> FromValue<TKey, TValue>(TKey key, TValue value)
        {
            return new KeyValue<TKey, TValue>()
            {
                Key = key,
                Value = value,
            };
        }

        #endregion
    }
}
